class CountDown {
    constructor (elem, endTime) {
        this.elem = elem;
        this.endTime = endTime;
        this.setCountDown();
    }
    countDown () {
        let offsetTime = parseInt(this.endTime - new Date().getTime() / 1000);

        let hour = this.constructor.numFormat(parseInt(offsetTime / (60 * 60)));
        let minute = this.constructor.numFormat(parseInt(offsetTime / 60 % 60));
        let seconds = this.constructor.numFormat(parseInt(offsetTime % 60));

        if (offsetTime < 0) {
            return false
        }

        return {
            hour,
            minute,
            seconds
        }
    }
    setCountDown () {
        setTimeout(() => {
            let timeObj = this.countDown();
            if (timeObj) {
                let {hour, minute, seconds} = timeObj;
                this.elem.find('.hour').text(hour);
                this.elem.find('.minute').text(minute);
                this.elem.find('.second').text(seconds);
                this.setCountDown();
            }
        }, 1000);
    }
    static numFormat (num) {
        return num < 10 ? `0${num}` : num;
    }
}